
#include "comLogManager.h"
#include "TemplateMgr.h"
#include "ttcodeStateChart.h"
#include "templateLuaMgr.h"


#include <boost/filesystem.hpp>
#include <boost/foreach.hpp>
#include <boost/regex.hpp>





TemplateMgr::TemplateMgr()
:m_LocalTempPath(globalTTEnv["tt_home"] + "/tt-app/templates")
,m_Output("")
{
	this->searchLocal();
}
void TemplateMgr::loadProjectFile( const string & out_put )
{
	m_Output = out_put;
	ttcode_log << "m_Output=" << m_Output << endl;
	try
	{
	}
	catch(std::exception e)
	{
		ttcode_log << "exception:" << e.what() << endl;
	}
}

void TemplateMgr::genProject(const string & uuid,const string &doc_name,const string &doc_path)
{
	ttTempList list = globalTempMgr->getTemp();
	string luaPath;
	BOOST_FOREACH( TiXmlElement *elem,list )
	{
		TiXmlElement *id = elem->FirstChildElement("id");
		if( NULL == id )
		{
			continue;
		}
		const char * pId = id->GetText();
		if( NULL == pId )
		{
			continue;
		}
		string strId(pId);
		ttcode_log << "strId=<"<< strId << ">" << endl;
		if( uuid != strId )
		{
			continue;
		}
		TiXmlElement *script = elem->FirstChildElement("script");
		if( NULL == script )
		{
			break;
		}
		TiXmlElement *project = script->FirstChildElement("project");
		if( NULL == project )
		{
			break;
		}
		const char * pProject = project->GetText();
		if( NULL == pProject )
		{
			break;
		}
		luaPath = string(pProject);
		break;
	}
	if( !luaPath.empty() )
	{
		using namespace boost;
		using namespace boost::filesystem;
		path templPath( m_Paths[uuid] + "/" + luaPath );
		if( exists(templPath) )
		{
			std::string script;
			script += "globalProjectName=\"";
			script += doc_name;
			script += "\"\n";
			script += "globalProjectPath=\"";
			script += doc_path;
			script += "\"\n";

			templateLuaMgr luaMgr(globalTTEnv["tt_home"]);
			luaMgr.callLuaString( script );
			luaMgr.callLuaApp( templPath.string() );
		}
	}
}



void TemplateMgr::searchLocal(void)
{
	using namespace boost;
	using namespace boost::filesystem;
	path templPath(m_LocalTempPath);
	if( false == is_directory(templPath))
	{
		return;
	}
	directory_iterator itDir(templPath);
	directory_iterator Dir_end;
	for ( ; itDir != Dir_end ; itDir++ )
	{
		regex reg("\\.template$");
		smatch result;
		string tmpPathStr = itDir->path().string();
		if(regex_search(tmpPathStr,result,reg))
		{
			ttcode_log << "tmpPathStr=" << tmpPathStr << endl;
			path config(tmpPathStr + "/configure");
			if( is_regular_file(config) )
			{
				TiXmlDocument *xml = new TiXmlDocument(config.string());
				xml->LoadFile();
				TiXmlElement *root = xml->RootElement();
				m_Temp.push_back( root );
				TiXmlElement *id = root->FirstChildElement("id");
				if( NULL == id )
				{
					continue;
				}
				const char * pId = id->GetText();
				if( NULL == pId )
				{
					continue;
				}
				string strId(pId);
				m_Paths[pId] = itDir->path().string();
			}
		}
	}
}

